package com.music.activity;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Fragment;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.music.R;
import com.music.adapter.NetMusicListAdapter;
import com.music.utilbean.Contant;
import com.music.utilbean.SearchResult;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.util.ArrayList;
@SuppressLint("NewApi")
public class NetBDMusicFragment extends Fragment implements OnItemClickListener{
	private Tab3ActivityMusic tab3Activity;
	private ListView          netMusic_list;
	private ArrayList<SearchResult> searchList=new ArrayList<SearchResult>();
	public static NetMusicListAdapter netMusicListAdapter;

	public static NetBDMusicFragment newInstance() {
		NetBDMusicFragment netmusicfragment=new NetBDMusicFragment();
		return netmusicfragment;
		
	}
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
	tab3Activity=(Tab3ActivityMusic)getActivity();
	}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
		Bundle savedInstanceState) {
	View view=inflater.inflate(R.layout.net_musiclist,null);
	netMusic_list=(ListView) view.findViewById(R.id.net_listview);

	
	netMusic_list.setOnItemClickListener(this);
	
	loadnetData();
	return view;
}
@Override
public void onResume() {
	// TODO Auto-generated method stub
	super.onResume();
	//tab3Activity.bindplayService();
}
@Override
public void onPause() {
	// TODO Auto-generated method stub
	super.onPause();
	//tab3Activity.unbindplayService();
}
private void loadnetData(){
	new LoadNetDataTask().execute(Contant.BAIDU_URL+Contant.BAIDU_DAYHOT);
}
//加载网络音乐的异步任务
class LoadNetDataTask extends AsyncTask<String,Void,Integer>{
    @Override
    protected void onPreExecute() {
	super.onPreExecute();
	searchList.clear();
}
   
	@Override
	protected Integer doInBackground(String... params) {
	String url=params[0];
	try {//jsoup解析网络音乐，根据百度音乐网页信息截取
		Document doc=Jsoup.connect(url).userAgent(Contant.USER_AGENT).timeout(6*1000).get();
		Elements songTitles=doc.select("span.song-title");
		Elements artists=doc.select("span.author_list");
		for (int i = 0; i < songTitles.size(); i++) {
			SearchResult searchResult=new SearchResult();
			Elements urls=songTitles.get(i).getElementsByTag("a");
			searchResult.setUrl(urls.get(0).attr("href"));
			searchResult.setMusicname(urls.get(0).text());
			Elements artistElements=artists.get(i).getElementsByTag("a");
			searchResult.setArtist(artistElements.get(0).text());
			searchResult.setAlbum("热歌榜");
			searchList.add(searchResult);
		}
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		return -1;
	}
		return 1;
	}
	@Override
		protected void onPostExecute(Integer result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			if(result==1){
				netMusicListAdapter=new NetMusicListAdapter(tab3Activity,searchList);
			ImageView loading_img=(ImageView) tab3Activity.findViewById(R.id.loading_img);
			loading_img.setVisibility(View.GONE);//设置加载动态图标为不可见、不会占用空位
			netMusic_list.setAdapter(netMusicListAdapter);
			netMusic_list.addFooterView(LayoutInflater.from(tab3Activity).inflate(R.layout.footview_layout, null));
			}
			Toast.makeText(tab3Activity, "请确保数据连接", Toast.LENGTH_SHORT).show();
		}
}

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
	//在线播放
	if (arg2>=netMusicListAdapter.getSearchResults().size()||arg2<0) {
		return;
	}
	showDownloadDialog(arg2);
}
//下载的弹窗
private void showDownloadDialog(final int position) {
	DownLoadMusicFragment downLoadMusicFragment=DownLoadMusicFragment.newInstance(netMusicListAdapter.getSearchResults().get(position));
	downLoadMusicFragment.show(getFragmentManager(),"download");
}

}
